<?php
class Menu {
	function gen($links = array(), $wrapper_start = null, $wrapper_end = null) {
		$obj =& get_instance();
		$obj->load->helper('url');
		
		if (count($links) > 0) {
			$menu = $this->genMenu($links, $wrapper_start, $wrapper_end);
		} else {
			$menu = array(
				'left_menu' => $this->genMenu($obj->config->item('left_menu')),
				'middle_menu' => $this->genMenu($obj->config->item('middle_menu'), '<div class="special-nav-left-a">', '</div><div class="special-nav-right"></div>'),
				'right_menu' => $this->genMenu($obj->config->item('right_menu')),
				'login_menu' => $this->genMenu($obj->config->item('login_menu')),
				'footer_menu' => $this->genMenu($obj->config->item('footer_menu')),
				'l_left_menu' => $this->genMenu($obj->config->item('l_left_menu'), '<div class="special-nav-left-b">', '</div><div class="special-nav-right"></div>'),
				'l_right_menu' => $this->genMenu($obj->config->item('l_right_menu')),
				'adm_left_menu' => $this->genMenu($obj->config->item('adm_left_menu'), '<div class="special-nav-left-c">', '</div><div class="special-nav-right"></div>'),
				'adm_right_menu' => $this->genMenu($obj->config->item('adm_right_menu'))
			);
		}
		
		return $menu;
	}
	
	function genMenu($links = array(), $wrapper_start = null, $wrapper_end = null) {
		$menu = "";
		
		if ($wrapper_start) {
			$menu .= $wrapper_start;
		}
		
		$menu .= "<ul>";
		foreach ($links as $text => $link) {
			if (is_array($link)) {
				// submenu
				$menu .= '<li class="dropdown">';
				$menu .= '<a href="">'.$text.'</a>';
				$menu .= '<ul class="subnav"><span class="subnav-mid">';
				foreach ($link as $t => $l) {
					$menu .= '<li>'.anchor($l, $t).'</li>';
				}
				$menu .= '<span class="clr"></span></span></ul>';
				$menu .= "</li>";
			} else {
				if ($link == uri_string()) {
					$menu .= '<li class="active">';
				} else {
					$menu .= "<li>";
				}
				$menu .= anchor($link, $text);
				$menu .= "</li>";
			}
		}
		$menu .= "</ul>";
	
		if ($wrapper_end) {
			$menu .= $wrapper_end;
		}
		
		return $menu;
	}
}
?>